// 筛选数据对象
const ScreenData = {
    AllSelect(selectData){
        selectData = this.sexSelect(selectData);
        selectData = this.ageSelect(selectData);
        selectData = this.sortSelect(selectData);
        return selectData;
    },

    // sex筛选
    sexSelect(selectData){
        let i = this.sexIndexFn();
        let arr = [];
        selectData.forEach(element => {
            i === 0 && arr.push(element);
            if(element.userSex == J_sex.children[i].innerHTML){
                arr.push(element);
            }
        });
        return arr;
    },

    // 判断sex谁被选中
    sexIndexFn(){
        for(let i = 0,len = J_sex.children.length; i < len ;i++){
            if(J_sex.children[i].className === 'active'){
                return i;
            }
        }
    },

    // age筛选
    ageSelect(selectData) {  
        let i = this.ageIndexFn();
        let arr = [];
        switch(i){
            case 0:
                selectData.forEach(element => {
                    arr.push(element);
                });
                break;
            case 1:
                selectData.forEach(element => {
                    if(element.userAge <= 18){
                        arr.push(element);
                    }
                });
                break;
            case 2:
                selectData.forEach(element => {
                    if(element.userAge > 18 && element.userAge <= 25){
                        arr.push(element);
                    }
                });
                break;
            case 3:
                selectData.forEach(element => {
                    if(element.userAge > 25 && element.userAge <= 35){
                        arr.push(element);
                    }
                });
                break;
            case 4:
                selectData.forEach(element => {
                    if(element.userAge > 35){
                        arr.push(element);
                    }
                });
                break;
        }
        return arr;
    },

    // 判断age谁被选中
    ageIndexFn(){
        for(let i = 0,len = J_age.children.length; i < len ;i++){
            if(J_age.children[i].className === 'active'){
                return i;
            }
        }
    },

    // 排序
    sortSelect(selectData){  
        let obj = this.sortIndexFn();
        switch(obj.i){
            case 0:
                if(obj.value === 'asc'){
                    selectData.sort((a,b) => a.userId - b.userId);
                }else{
                    selectData.sort((a,b) => b.userId - a.userId);
                }
                break;
            case 1:
                if(obj.value === 'asc'){
                    selectData.sort((a,b) => a.userName.localeCompare(b.userName, 'zh-CN'));
                }else{
                    selectData.sort((a,b) => b.userName.localeCompare(a.userName, 'zh-CN'));
                }
                break;
            case 2:
                if(obj.value === 'asc'){
                    selectData.sort((a,b) => a.userAge - b.userAge);
                }else{
                    selectData.sort((a,b) => b.userAge - a.userAge);
                }
                break;
        }
        return selectData;
    },

    // 获取选中的下标和状态
    sortIndexFn(){
        for(let i = 0,len = J_sort.children.length; i < len ;i++){
            if(J_sort.children[i].className === 'active'){
                let value = J_sort.children[i].dataset.sort;
                return {
                    i,
                    value
                };
            }
        }
    },

    // 搜索
    screenFn(){
        let J_search = document.getElementById('J_search');
        J_search.onkeydown = function (e) {   
            if(e.keyCode === 13){
                let arr = [];
                let key = J_search.value;
                if(/^[\u4e00-\u9fa5]{2,4}$/.test(key)){
                    Data.forEach(element => {
                        if(element.userName == key){
                            arr.push(element);
                        }
                    });
                }else if(/^1[34578]\d{9}$/.test(key)){
                    Data.forEach(element => {
                        if(element.userPhone == key){
                            arr.push(element);
                        }
                    });
                }else if(/^2020\d{5}$/.test(key)){
                    Data.forEach(element => {
                        if(element.userId == key){
                            arr.push(element);
                        }
                    });
                }

                J_search.value = '';
                Renderer.rendererData(index, arr);
                ScreenBtn.pages(arr)
            }
        }
    }
}